<?php

class Zcache
{
	static function cache($name){
		$cache=array(
			'cookie_',
			
			'session_',
			'session_site_sysconfig',				//站点参数配置
		
			/**
			 * 文件缓存
			 */
			'file_site_sysconfig',					//站点参数配置
			'file_share_label_indexshow',			//分享模块 在首页上显示的所有标签
			'file_share_label_is_fixed_cat',		//分享模块 能确定商品类别的所有标签
			'file_share_label',						//分享模块 所有标签
			'file_gather_url_XXXX',					//采集网址数据 XXXX 类似 http://www.meilishuo.com/item/2167621?wzz=p0r0c2
		);
	}
	
	
	static function setCacheLabel($name,$key,$cache_type){
		$condition=" cache_name='".$name."' AND cache_key='".$key."' AND cache_type='".$cache_type."'";
		$model=SystemCache::model()->find($condition);
		$ctime=Func::getCurrentTime();
		if($model===null){
			$model=new SystemCache();
			$model->cache_name=$name;
			$model->cache_key=$key;
			$model->cache_type=$cache_type;
			$model->update_created=$ctime;
			$model->last_created=$ctime;
			return $model->save();
		}else{
			
			$model->last_created=$model->update_created;
			$model->update_created=$ctime;
			return $model->save();
		}
	}
        
        static function cacheRegionJS(){
                $cacheFile = YII::getPathOfAlias("webroot")."/statics/js/region.js";
                $cacheTime = 3600*24;
                if(file_exists($cacheFile) && time()-filemtime($cacheFile)<$cacheTime)
                    return true;
                $regionTab = new Region();
                $data = $regionTab->findAll('region_type > 0');
                $var_sp = "\n\n\n";
                $cache_data = 'var region_data = []';
                $v1_data = 'region_data[1] = \'';
                $v2_data = 'region_data[2] = \'';
                $v3_data = 'region_data[3] = \'';
                foreach($data AS $k => $v){
                    $var_name = "v{$v['region_type']}_data";
                    $$var_name .= "@{$v['region_id']}`!{$v['region_name']}`!{$v['parent_id']}$";
                }
                $cache_data = $cache_data.";".$var_sp.$v1_data."';".$var_sp.$v2_data."';".$var_sp.$v3_data."';".$var_sp;
                return file_put_contents($cacheFile, $cache_data); 
        }
}
